package org.example;
import java.util.Scanner;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:何人亮
 * Date:2024-10-18
 * 18:35
 */
public class Test {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int row=sc.nextInt();
        int col=sc.nextInt();
        int[][] arr=new int[row+1][col+1];
        String ss1=sc.nextLine();
        for (int i = 0; i < row; i++) {


            String s=sc.nextLine();
            String[] ss=s.split(" ");
            for (int j = 0; j < col; j++) {
                int k=Integer.parseInt(ss[j]);
                arr[i][j]=k;
            }
        }

        for (int i = 0; i < row ; i++) {//行
            int sum=0;
            for (int j = 0; j < col ; j++) {//列
                sum+=arr[i][j];
            }
            arr[i][col]=sum;
        }
        for (int i = 0; i < col+1 ; i++) {//列
            int sum=0;
            for (int j = 0; j < row ; j++) {//行
                sum+=arr[j][i];
            }
            arr[row][i]=sum;
        }
        for (int i = 0; i < row+1; i++) {
            for (int j = 0; j < col+1; j++) {
                if(j==col){
                    System.out.print(arr[i][j]);
                }else{
                    System.out.print(arr[i][j]+" ");
                }
            }
            System.out.println();
        }
    }
}
